import functools
class Solution(object):
    def canCross(self, stones):
        """
        :type stones: List[int]
        :rtype: bool
        """
        s = set(stones)
        end = stones[-1]

        @functools.cache
        def search(k, t):
            if t == end:
                return True
            else:
                if t + k in s:
                    if search(k, t + k):
                        return True
                if t + k + 1 in s:
                    if search(k + 1, t + k + 1):
                        return True
                if k - 1 > 0 and t + k - 1 in s:
                    if search(k - 1, t + k - 1):
                        return True
                return False

        if 1 in s:
            return search(1, 1)
        else:
            return False

data = Solution()
stones = [0, 1, 3, 5, 6, 8, 12, 17]
print(data.canCross(stones))
